package com.fishingwithme.android.ui.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fishingwithme.android.data.location.LocationService
import com.fishingwithme.android.data.model.Line
import com.fishingwithme.android.data.repository.FishingRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

data class NavigatorUiState(
    val isLoading: Boolean = false,
    val navLine: Line? = null,
    val error: String? = null
)

@HiltViewModel
class NavigatorViewModel @Inject constructor(
    private val repository: FishingRepository,
    private val locationService: LocationService
) : ViewModel() {

    private val _uiState = MutableStateFlow(NavigatorUiState())
    val uiState: StateFlow<NavigatorUiState> = _uiState.asStateFlow()

    // 获取钓点导航信息
    fun fetchSpotNavigationInfo(spotId: String) {
        viewModelScope.launch {
            _uiState.value = _uiState.value.copy(isLoading = true, error = null)
            
            try {
                // 调用repository获取钓点导航信息
                val line = repository.loadSpotNavigationInfo(spotId)
                _uiState.value = _uiState.value.copy(
                    isLoading = false,
                    navLine = line
                )
            } catch (e: Exception) {
                _uiState.value = _uiState.value.copy(
                    isLoading = false,
                    error = e.message ?: "网络请求异常"
                )
            }
        }
    }

    // 清除错误信息
    fun clearError() {
        _uiState.value = _uiState.value.copy(error = null)
    }

}